// packageE/location/location.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    },
    page_id: {
      type: null
    }
  },
  data: {
    address: "",
    point: {
      lat: '',
      lng: ''
    },
    city: ""
  }, // 私有数据，可用于模板渲染

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {

    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this._getLocation((mapdata,e)=>{
      if(this.data.datas.ad_name === '1') {
        let {province,city,district} = mapdata.address_component;
        this.data.address = province ? (province + city + district) : district;
      } else {
        this.data.address = mapdata.address;
      }
      this.setData({
        address: this.data.address,
        city: mapdata.address_component.city ? mapdata.address_component.city : mapdata.address_component.province,
        point: e
      });
      this.setLocationLog(mapdata.ad_info.adcode);//记录当天定位的区域信息，可用于统计区域热度（h5没对接）
      this.yz_basic_info = wx.getStorageSync('yz_basic_info');
      if(!this.yz_basic_info?.plugin_setting?.address_relation?.open_state) return;//没开启城市关联
      if(this.data.datas.is_open_city_page){
        let adcode = app.globalData.CityPageADCode;
        if(adcode === mapdata.ad_info.adcode) return;
        app.globalData.CityPageADCode = mapdata.ad_info.adcode;
        // 判断是否开启城市关联，有则传城市区域编码给装修接口
        // 同时需要和之前记录的区域编码比较，当有变动时，重新请求更新首页装修数据
        this.triggerEvent('locationSuccess', { address_code: mapdata.ad_info.adcode });
      }
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },

  methods: {
    tolocation() {
      const pages = getCurrentPages();
      const currentPage = pages[pages.length - 1];
      const url = `/${currentPage.route}?page_id=${this.data.page_id}`;
      wx.setStorageSync('diyurl', url);
      let tag = 'shop';
      if(this.yz_basic_info?.plugin_setting?.address_relation?.open_state && this.data.datas.location_tag) tag = this.data.datas.location_tag;
      wx.navigateTo({
        url: `/packageC/o2o/o2oLocation/o2oLocation?tag=${tag}`
      });

    },
    toBack(){
      wx.navigateBack({
        delta:1
      })
    },
    tosearch(e) {
      if (app.globalData.store_alone_temp == 1) {
        // 开启门店独立模块
        wx.navigateTo({
          url: '/packageC/o2o/StoreSearch/StoreSearch?store_id=' + app.globalData.STORE_ID
        });
        return;
      }
      let key = e.currentTarget.dataset?.item || false;
      if (!this.data.city && !key) {
        wx.showToast({
          icon: 'none',
          title: '请先选择城市',
          duration: 1000
        });
        return;
      }
      let search_type = this.data.datas.search_type || 'store';
      wx.navigateTo({
        url: '/packageC/o2o/o2oSearchMultiple/o2oSearchMultiple?city=' + this.data.city + '&point=' + JSON.stringify(this.data.point)+'&search_type='+search_type
      });
    },
    setLocationLog(adcode) {
      app._getNetWork({
        url: app.getNetAddresss('home-page.get-location-info'),
        data: { adcode: adcode },
        success: ({data}) => {},
        fail: function (res) {
          console.log(res);
        }
      })
    },
  }
});
